package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:test
 * @Create by: CAt
 * @Date;2021/10/512:47
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;


    //t添加套餐
    @PostMapping
    public R  post(@RequestBody SetmealDto setmealDto){
        setmealService.tianjia(setmealDto);
        return R.success("添加成功");
    }
    //分页查询
    @GetMapping("/page")
    public R fenye(Integer page,Integer pageSize,String name){
        IPage<Setmeal> pageif=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> querwwad=new LambdaQueryWrapper<>();
        querwwad.like(name!=null,Setmeal::getName,name);
        querwwad.orderByAsc(Setmeal::getUpdateTime);
        IPage<Setmeal> page1 = setmealService.page(pageif, querwwad);
        return R.success(page1);
    }
    //修改套餐回显
    @GetMapping("/{id}")
    public R huixan(@PathVariable Long id){
        System.out.println(id);
        log.info("{} ",id);
        SetmealDto xiugaihuixian = setmealService.xiugaihuixian(id);
        return R.success(xiugaihuixian);
    }
    //修改套餐回显后保存
    @PutMapping
    public R baocun(@RequestBody SetmealDto setmealDto){
        setmealService.huixianbaocun(setmealDto);
        return R.success("保存成功");
    }
    //停售和起售
    @PostMapping("/status/{status}")
    public R tingshou(@PathVariable Integer status, Long[] ids){
        System.out.println(status);
        System.out.println(ids);
        LambdaUpdateWrapper<Setmeal> aaa=new LambdaUpdateWrapper<>();
        aaa.set(status!=null,Setmeal::getStatus,status);

        for (Long id : ids) {
            aaa.eq(id!=null,Setmeal::getId,id);
            setmealService.update(aaa);
        }
        return R.success("成功");
    }
}
